/*
 * Copyright (C) 2013 The original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.zapta.apps.maniana.persistence;

import org.json.JSONException;
import org.json.JSONObject;

import com.zapta.apps.maniana.annotations.ApplicationScope;
import com.zapta.apps.maniana.model.ItemModel;

/**
 * Interface to ItemModel's StoreIF using JSON for saving/restoring
 *
 * @author Elliott Mitchell
 */
@ApplicationScope
public class StoreIF implements ItemModel.StoreIF {

	/** The JSON object we're interfacing with. */
	private final JSONObject jObj;

	StoreIF(JSONObject _jObj) {
		jObj=_jObj;
	}

	@Override
	public void setString(final String key, final String value) {
		try {
			jObj.put(key, value);
		} catch(JSONException e) {
			throw new RuntimeException("JSON serialization error", e);
		}
	}
	@Override
	public void setBoolean(final String key, final boolean value) {
		try {
			jObj.put(key, value);
		} catch(JSONException e) {
			throw new RuntimeException("JSON serialization error", e);
		}
	}
	@Override
	public void setByte(final String key, final byte value) {
		try {
			jObj.put(key, value);
		} catch(JSONException e) {
			throw new RuntimeException("JSON serialization error", e);
		}
	}
	@Override
	public void setShort(final String key, final short value) {
		try {
			jObj.put(key, value);
		} catch(JSONException e) {
			throw new RuntimeException("JSON serialization error", e);
		}
	}
	@Override
	public void setInt(final String key, final int value) {
		try {
			jObj.put(key, value);
		} catch(JSONException e) {
			throw new RuntimeException("JSON serialization error", e);
		}
	}
	@Override
	public void setLong(final String key, final long value) {
		try {
			jObj.put(key, value);
		} catch(JSONException e) {
			throw new RuntimeException("JSON serialization error", e);
		}
	}

	@Override
	public String getString(final String key) {
		return jObj.optString(key);
	}
	@Override
	public boolean getBoolean(final String key) {
		return jObj.optBoolean(key, false);
	}
	@Override
	public byte getByte(final String key) {
		return (byte)jObj.optInt(key, 0);
	}
	@Override
	public short getShort(final String key) {
		return (short)jObj.optInt(key, 0);
	}
	@Override
	public int getInt(final String key) {
		return jObj.optInt(key, 0);
	}
	@Override
	public long getLong(final String key) {
		return jObj.optLong(key, 0);
	}

	@Override
	public boolean exists(final String key) {
		return jObj.has(key);
	}
	@Override
	public void delete(final String key) {
		jObj.remove(key);
	}
}
